 package my.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import af.spring.AfRestData;
import af.spring.AfRestError;
import my.db.Clazz;
import my.db.Student;
import my.db.Teacher;
import my.db.TerCrs;
import my.tool.MybatisApi;

@Controller
public class UserController {

	@GetMapping("user")
	public String hello(Model model,HttpServletRequest request) {
		
		List<Map<String , Object>> TeacherASList = MybatisApi.selectList("zhy.ses.getTeacherAS", 0);
		List<Teacher> TeacherList = MybatisApi.selectList("zhy.ses.getTeachers", 0);
		List<Clazz> ClazzList = MybatisApi.selectList("zhy.ses.getClazzs", Clazz.class);
		List<Map<String, Object>> A_C_StudentList = MybatisApi.selectList("zhy.ses.getA_C_students", 0);
		List<Map<String, Object>> Academy = MybatisApi.selectList("zhy.ses.getAcademys", 0);
		model.addAttribute("ClazzList", ClazzList);
		model.addAttribute("Academy", Academy);
		model.addAttribute("TeacherASList", TeacherASList);
		model.addAttribute("TeacherList", TeacherList);
		model.addAttribute("A_C_StudentList", A_C_StudentList);
		model.addAttribute("TeacherCount", TeacherList.size());
		model.addAttribute("datas",IndexController.SimpleJS(request));
		return "User_Manager";
	}
	
//	保存学生信息
	@PostMapping("index_saveStudent.do")
	public Object saveStudent(@RequestBody JSONObject jreq) {

		Student stu = new Student();
		stu.setRealName(jreq.getString("RealName"));
		stu.setStudentId(jreq.getInteger("StudentId"));
		stu.setPassword(jreq.getString("Password"));
		stu.setSex(jreq.getBoolean("Sex"));
		stu.setClazzId(jreq.getInteger("ClazzId"));
		MybatisApi.insert("zhy.ses.saveStudent", stu);
		return new AfRestData("");
	}
	
//	保存教师信息
	@PostMapping("index_saveTeacher.do")
	public Object saveTeacher(@RequestBody JSONObject jreq) {
		
		
		Teacher teacher = new Teacher();
		teacher.setRealName(jreq.getString("RealName"));
		teacher.setTeacherId(jreq.getInteger("TeacherId"));
		teacher.setPassword(jreq.getString("Password"));
		teacher.setSex(jreq.getBoolean("Sex"));
		teacher.setIsManager(false);
		teacher.setIsDean(false);
		
		MybatisApi.insert("zhy.ses.saveTeacher", teacher);
		TerCrs tcs = new TerCrs();
		tcs.setTeacher_id(teacher.getId());
		MybatisApi.insert("zhy.ses.saveTerCrs", tcs);
		return new AfRestData("");
	}
	

//	保存修改后的教师信息
	@PostMapping("index_saveUpdateTeacher.do")
	public Object saveUpdateTeacher(@RequestBody JSONArray jarry) {
		
		for(int i=0;i<jarry.size();i++) {
			Teacher teacher = jarry.getObject(i, Teacher.class);
			if(teacher.getAcademyId() != teacher.getOldAcademyId() && teacher.getIsDean()) {
				teacher.setIsDean(false);
				MybatisApi.update("zhy.ses.updateAcademyDean", teacher.getOldAcademyId());
			}
			
			MybatisApi.update("zhy.ses.updateTeacher", teacher);
		}
		return new AfRestData("");
	}
	
//	保存修改后的学生信息
	@PostMapping("index_saveUpdateStudent.do")
	public Object saveUpdateStudent(@RequestBody JSONArray jarry) {
		
			for(int i=0;i<jarry.size();i++) {
				
				MybatisApi.update("zhy.ses.updateStudent", jarry.getObject(i, Student.class));
			}
		return new AfRestData("");
	}
	
//	删除信息
	@PostMapping("index_deleteTeacher.do")
	public Object deleteTeacher(@RequestBody JSONArray jreqs) {

			for(int i = 0;i<jreqs.size();i++) {
				
				MybatisApi.delete("zhy.ses.deleteTeacher", jreqs.get(i));
			}

		return new AfRestData("");
	}
	
	@PostMapping("index_deleteStudent.do")
	public Object deleteStudent(@RequestBody JSONArray jreqs) {
		for(int i = 0;i<jreqs.size();i++) {
			
			MybatisApi.delete("zhy.ses.deleteStudent", jreqs.get(i));
		}
		
		return new AfRestData("");
	}
}
